package cn.jja8.bukkit.bukkitcommandtool.command;

import org.bukkit.configuration.ConfigurationSection;

import java.util.List;

/**
 * 一个ConfigurationSection的封装
 * 在获取某值的时候如果不存在就设置默认值
 * */
public class ConfigurationSectionSetDefGet {

    final ConfigurationSection configurationSection;

    public ConfigurationSectionSetDefGet(ConfigurationSection configurationSection) {
        this.configurationSection = configurationSection;
    }

    public String getStringSetDef(String key,String def){
        if (!configurationSection.contains(key)) {
            configurationSection.set(key,def);
            return def;
        }else {
            return configurationSection.getString(key);
        }
    }

    public int getIntSetDef(String key,int def){
        if (!configurationSection.contains(key)) {
            configurationSection.set(key,def);
            return def;
        }else {
            return configurationSection.getInt(key);
        }
    }

    public long getLongSetDef(String key,long def){
        if (!configurationSection.contains(key)) {
            configurationSection.set(key,def);
            return def;
        }else {
            return configurationSection.getLong(key);
        }
    }

    public double getDoubleSetDef(String key,double def){
        if (!configurationSection.contains(key)) {
            configurationSection.set(key,def);
            return def;
        }else {
            return configurationSection.getDouble(key);
        }
    }

    public boolean getBooleanSetDef(String key,boolean def){
        if (!configurationSection.contains(key)) {
            configurationSection.set(key,def);
            return def;
        }else {
            return configurationSection.getBoolean(key);
        }
    }

    public List<String> getStringListSetDef(String key, List<String> def){
        if (!configurationSection.contains(key)) {
            configurationSection.set(key,def);
            return def;
        }else {
            return configurationSection.getStringList(key);
        }
    }

    public <T extends ConfigurationSectionCanLoad> T getObjectSetDef(String k, T obg){
        if (!configurationSection.contains(k)) {
            obg.load(new ConfigurationSectionSetDefGet(configurationSection.createSection(k)));
        }else {
            obg.load(new ConfigurationSectionSetDefGet(configurationSection.getConfigurationSection(k)));
        }
        return obg;
    }
}
